package com.hjs.test.test;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author hongjs
 * @version 1.0.0
 * @date 2025年09月25日 16:10
 * @description 类描述
 */
public class HttpXmlClient {
    public static void main(String[] args) {
        // 接口地址
        String apiUrl = "http://200.100.103.107:8090/service/XChangeServlet?account=ncc0820";
        // XML文件路径
        String xmlFilePath = "C:\\Users\\Lenovo\\Desktop\\NC\\扫码券-领用.xml";
        //String xmlFilePath = "C:\\Users\\Lenovo\\Desktop\\NC\\扫码券-回款-现销.xml";
        try {
            // 读取XML文件内容
            //String xmlContent = readXmlFile(xmlFilePath);
            String xmlContent = XmlHelper.getVoucherXml();
            if (xmlContent == null || xmlContent.isEmpty()) {
                System.out.println("XML内容为空，无法发送请求");
                return;
            }
            System.out.println("xml参数: \n" + xmlContent);
            // 发送POST请求
            String response = sendPostRequest(apiUrl, xmlContent);
            System.out.println("接口响应: \n" + response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取XML文件内容
     */
    private static String readXmlFile(String filePath) {
        StringBuilder xmlContent = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                xmlContent.append(line).append("\n");
            }
        } catch (IOException e) {
            System.err.println("读取XML文件失败: " + e.getMessage());
            return null;
        }
        return xmlContent.toString();
    }

    /**
     * 发送POST请求并携带XML数据
     */
    private static String sendPostRequest(String urlStr, String xmlData) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法
        connection.setRequestMethod("POST");
        // 允许输入输出
        connection.setDoOutput(true);
        connection.setDoInput(true);
        // 设置Content-Type为XML
        connection.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
        // 设置内容长度
        connection.setRequestProperty("Content-Length", String.valueOf(xmlData.getBytes().length));

        // 写入XML数据
        try (OutputStream os = connection.getOutputStream()) {
            os.write(xmlData.getBytes("UTF-8"));
            os.flush();
        }

        // 读取响应
        StringBuilder response = new StringBuilder();
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line).append("\n");
                }
            }
        } else {
            response.append("请求失败，响应码: ").append(responseCode);
        }

        connection.disconnect();
        return response.toString();
    }
}
